package coml.jiyun.hp.gjtk_b.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.gjtk_b.R;
import coml.jiyun.hp.gjtk_b.bean.WanBean;

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private int LEFT=1;
    private int RIGHT=2;

    public HomeAdapter(Context context, ArrayList<WanBean.DataBean.DatasBean> datasBeans) {
        this.context = context;
        this.datasBeans = datasBeans;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == LEFT) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item1, null);
            return new ViewHolder1(inflate);
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item2, null);
            return new ViewHolder2(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = holder.getItemViewType();
        if (itemViewType == LEFT) {
            ViewHolder1 holder1 = (ViewHolder1) holder;
            Glide.with(context).load(datasBeans.get(position).getEnvelopePic()).into(holder1.item1_img);
            holder1.item1_tv1.setText(datasBeans.get(position).getTitle());
        } else {
            ViewHolder2 holder2 = (ViewHolder2) holder;
            holder2.item2_tv1.setText(datasBeans.get(position).getTitle());
            Glide.with(context).load(datasBeans.get(position).getEnvelopePic()).into(holder2.item2_img);

        }
    }
    @Override
    public int getItemCount() {
        return datasBeans.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 3 == 0){
            return LEFT;
        }else {
            return RIGHT;
        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder {

        ImageView item1_img;
        TextView item1_tv1;

        public ViewHolder1(@NonNull View itemView) {
            super(itemView);
            item1_img = itemView.findViewById(R.id.item1_img);
            item1_tv1 = itemView.findViewById(R.id.item1_tv1);
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        TextView item2_tv1;
        ImageView item2_img;

        public ViewHolder2(@NonNull View itemView) {
            super(itemView);
            item2_tv1 = itemView.findViewById(R.id.item2_tv1);
            item2_img = itemView.findViewById(R.id.item2_img);
        }
    }
}